//
//  LPFNetworkTools.swift
//  WeiBo
//
//  Created by mac on 16/4/18.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking

//请求方式枚举
enum LPFRequestMethod: Int{
    case GET = 0
    case POST = 1
}

class LPFNetworkTools: AFHTTPSessionManager {

    //网络全局访问点
    static let sharedTools: LPFNetworkTools = {
       let tools = LPFNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //网络请求方法
    func request(method: LPFRequestMethod, urlString: String,params: AnyObject?, callback: (response: AnyObject?, error: NSError?) -> ()){
        
        //GET
        if method == .GET{
            self.GET(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(response: nil, error: error)
            })
        }else{
           //POST
            self.POST(urlString, parameters: params, progress: nil, success: { (_, response) -> Void in
                callback(response: response, error: nil)
                
                }, failure: { (_, error) -> Void in
                    callback(response: nil, error: error)
            })
        }
        
    }
}
// MARK: - 请求微博首页数据
extension LPFNetworkTools{
    
    func loadStatus(accessToken: String, since_id: Int64, max_id: Int64, callback: (response: AnyObject?, error: NSError?) -> ()){
        // 请求url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        // 请求参数
        let params = [
            "access_token": accessToken,
            "since_id": "\(since_id)",
            "max_id": "\(max_id)"
        ]
        //发送请求
        request(LPFRequestMethod.GET, urlString: urlString, params: params) { (response, error) -> () in
            callback(response: response, error: error)
        }
    }
}